package com.dkts.oil.postgis;

import com.dkts.oil.model.AisDataPoint;
import com.dkts.oil.mysql.entity.AisPoint;
import com.dkts.oil.mysql.entity.ShipInfo;
import com.dkts.oil.mysql.entity.ShipTrackSegments;
import org.springframework.beans.BeanUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AisPointBatchInsert {

    private static final String INSERT_SQL =
            "INSERT INTO public.ais_data_point (" +
                    "mmsi, segment_id, dest, eta, lon, lat, hdg, cog, speed, rot, " +
                    "time, eta_std, navistat, distance, time_interval, uidkey, geom,segment_index) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ST_SetSRID(ST_MakePoint(?, ?), 4326),?)";

    /**
     * 批量插入 ais_data_point 数据
     *
     * @param points 数据点列表
     * @throws SQLException
     */
    public static void batchInsert(List<AisDataPoint> points) throws SQLException {
        try (Connection conn = PostgresConnectionUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(INSERT_SQL)) {
            int batchSize = 0;

            for (AisDataPoint p : points) {
                if (p.getMmsi() != null) {
                    ps.setLong(1, p.getMmsi());
                } else {
                    ps.setNull(1, Types.BIGINT);
                }

                if (p.getSegmentId() != null) {
                    ps.setInt(2, p.getSegmentId());
                } else {
                    ps.setNull(2, Types.INTEGER);
                }

                ps.setString(3, p.getDest());
                ps.setString(4, p.getEta());

                ps.setDouble(5, p.getLon());
                ps.setDouble(6, p.getLat());

                if (p.getHdg() != null) {
                    ps.setDouble(7, p.getHdg());
                } else {
                    ps.setNull(7, Types.DOUBLE);
                }

                if (p.getCog() != null) {
                    ps.setDouble(8, p.getCog());
                } else {
                    ps.setNull(8, Types.DOUBLE);
                }

                if (p.getSpeed() != null) {
                    ps.setDouble(9, p.getSpeed());
                } else {
                    ps.setNull(9, Types.DOUBLE);
                }

                if (p.getRot() != null) {
                    ps.setDouble(10, p.getRot());
                } else {
                    ps.setNull(10, Types.DOUBLE);
                }

                if (p.getTime() != null) {
                    ps.setTimestamp(11, p.getTime());
                } else {
                    ps.setNull(11, Types.TIMESTAMP);
                }

                if (p.getEtaStd() != null) {
                    ps.setTimestamp(12, p.getEtaStd());
                } else {
                    ps.setNull(12, Types.TIMESTAMP);
                }

                if (p.getNavistat() != null) {
                    ps.setInt(13, p.getNavistat());
                } else {
                    ps.setNull(13, Types.INTEGER);
                }

                if (p.getDistance() != null) {
                    ps.setDouble(14, p.getDistance());
                } else {
                    ps.setNull(14, Types.DOUBLE);
                }

                if (p.getTimeInterval() != null) {
                    ps.setDouble(15, p.getTimeInterval());
                } else {
                    ps.setNull(15, Types.DOUBLE);
                }

                ps.setString(16, p.getUidkey());

                // geom 坐标 lon, lat
                ps.setDouble(17, p.getLon());
                ps.setDouble(18, p.getLat());
                ps.setInt(19, p.getSegmentIndex());


                ps.addBatch();
                batchSize++;

                // 每1000条执行一次批处理，防止内存过大
                if (batchSize % 1000 == 0) {
                    ps.executeBatch();
                    ps.clearBatch();
                }
            }

            // 执行剩余批次
            if (batchSize % 1000 != 0) {
                ps.executeBatch();
                ps.clearBatch();
            }
        }
    }

    public static void addAisPoint(Map<String, List<AisPoint>> dataInfo, String uuid) {
        List<AisDataPoint> pointsAisData = new ArrayList<>();
        for (Map.Entry<String, List<AisPoint>> entry : dataInfo.entrySet()) {
            List<AisPoint> aisPoints = entry.getValue();
            int row = aisPoints.size();
            for (int i = 0; i < row; i++) {

                AisDataPoint aisDataPoint = new AisDataPoint();
                BeanUtils.copyProperties(aisPoints.get(i), aisDataPoint);
                aisDataPoint.setUidkey(uuid);
                aisDataPoint.setSegmentIndex(-1);
                aisDataPoint.setSegmentId(-1);
                pointsAisData.add(aisDataPoint);
            }
        }
        try {
            batchInsert(pointsAisData);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
